//
//  Common.m
//  sudic
//
//  Created by linqc on 16/9/16.
//  Copyright © 2016年 linqc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
#import "Common.h"
@import Darwin.Mach;


@implementation Common
+(NSString*)getWrittenFilePath:(NSString*)fileName {
    NSString* path = [self getWrittenFileDir];
    path = [path stringByAppendingPathComponent:fileName];
    return path;
}

+(NSString*)getWrittenFileDir {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
}


+ (id)objectWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
}

+ (NSString*)objectToJson:(id)dic
{
    if (!dic) return nil;
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

#define DEFAULT_VOID_COLOR [UIColor clearColor]
+ (UIColor *) colorWithHexString: (NSString *) stringToConvert
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters
    if ([cString length] < 6) return DEFAULT_VOID_COLOR;

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return DEFAULT_VOID_COLOR;
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];

    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];


    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

+ (void)addLayoutConstraintsWithViewController:(UIViewController *)viewController
                                   contentView:(UIView *)contentView
                                          left:(NSInteger)leftSpace
                                           top:(NSInteger)topSpace
                                         right:(NSInteger)rightSpace
                                        bottom:(NSInteger)bottomSpace
                                         width:(NSInteger)widthSpace
                                        height:(NSInteger)heightSpace
                                        hasNav:(BOOL)hasNavigation
{
    if (@available(iOS 11, *)) {
    } else {
        viewController.automaticallyAdjustsScrollViewInsets = NO;
    }
    //添加约束
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat navigationHeight = 0;
        if (hasNavigation && viewController.navigationController && viewController.navigationController.navigationBar)
            navigationHeight = viewController.navigationController.navigationBar.frame.size.height;
        int trueTopSpace = navigationHeight + [[UIApplication sharedApplication] statusBarFrame].size.height;
        if (topSpace) trueTopSpace += topSpace;
        
        contentView.translatesAutoresizingMaskIntoConstraints = NO;//使用autolayout
        if (@available(iOS 11, *)) {
            NSLayoutConstraint *top = nil;
            if (topSpace == 0) {
                top = [contentView.topAnchor constraintEqualToAnchor:viewController.view.safeAreaLayoutGuide.topAnchor];
            }
            else if (topSpace != INVLIAD_CONSTRAINT_VALUE) {
                top = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeTop multiplier:1 constant:topSpace == -1 ? 0 : trueTopSpace];
            }
            
            NSLayoutConstraint *bottom = nil;
            if (bottomSpace == 0) {
                bottom = [contentView.bottomAnchor constraintEqualToAnchor:viewController.view.safeAreaLayoutGuide.bottomAnchor constant:0];
            } else if (bottomSpace != INVLIAD_CONSTRAINT_VALUE) {
                bottom = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeBottom multiplier:1 constant:bottomSpace == -1 ? 0 : -bottomSpace];
            }
            
            NSLayoutConstraint *left = nil;
            if (leftSpace == 0) {
                left = [contentView.leadingAnchor constraintEqualToAnchor:viewController.view.safeAreaLayoutGuide.leadingAnchor];
            } else if (leftSpace != INVLIAD_CONSTRAINT_VALUE) {
                left = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeLeft multiplier:1 constant:leftSpace == -1 ? 0 : leftSpace];
            }
            
            NSLayoutConstraint *right = nil;
            if (rightSpace == 0) {
                right = [contentView.trailingAnchor constraintEqualToAnchor:viewController.view.safeAreaLayoutGuide.trailingAnchor];
            } else if (rightSpace != INVLIAD_CONSTRAINT_VALUE) {
                right = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeRight multiplier:1 constant:rightSpace == -1 ? 0 : -rightSpace];
            }
            
            NSLayoutConstraint* width = nil;
            if (widthSpace != INVLIAD_CONSTRAINT_VALUE){
                width = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:widthSpace];
            }
            
            NSLayoutConstraint* height = nil;
            if (heightSpace != INVLIAD_CONSTRAINT_VALUE){
                height = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:heightSpace];
            }
            
            NSMutableArray* arr = [[NSMutableArray alloc] init];
            if (top) [arr addObject:top];
            if (bottom) [arr addObject:bottom];
            if (left) [arr addObject:left];
            if (right) [arr addObject:right];
            if (width) [arr addObject:width];
            if (height) [arr addObject:height];
            [NSLayoutConstraint activateConstraints:arr];
        } else {
            NSLayoutConstraint *top = nil;
            if (topSpace == 0) {
                top = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeTop multiplier:1 constant:trueTopSpace];
            }
            else  if (topSpace != INVLIAD_CONSTRAINT_VALUE) {
                top = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeTop multiplier:1 constant:topSpace == -1 ? 0 : trueTopSpace];
            }
            
            NSLayoutConstraint *bottom = nil;
            if (bottomSpace == 0) {
                bottom = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
            } else  if (bottomSpace != INVLIAD_CONSTRAINT_VALUE) {
                bottom = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeBottom multiplier:1 constant:bottomSpace == -1 ? 0 : -bottomSpace];
            }
            
            NSLayoutConstraint *left = nil;
            if (leftSpace == 0) {
                left = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
            } else  if (leftSpace != INVLIAD_CONSTRAINT_VALUE) {
                left = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeLeft multiplier:1 constant:leftSpace == -1 ? 0 : leftSpace];
            }
            
            NSLayoutConstraint *right = nil;
            if (rightSpace == 0) {
                right = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeRight multiplier:1 constant:0];
            } else  if (rightSpace != INVLIAD_CONSTRAINT_VALUE) {
                right = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:viewController.view attribute:NSLayoutAttributeRight multiplier:1 constant:rightSpace == -1 ? 0 : -rightSpace];
            }
            
            NSLayoutConstraint* width = nil;
            if (widthSpace != INVLIAD_CONSTRAINT_VALUE){
                width = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:widthSpace];
            }
            
            NSLayoutConstraint* height = nil;
            if (heightSpace != INVLIAD_CONSTRAINT_VALUE){
                height = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:heightSpace];
            }
            
            NSMutableArray* arr = [[NSMutableArray alloc] init];
            if (top) [arr addObject:top];
            if (bottom) [arr addObject:bottom];
            if (left) [arr addObject:left];
            if (right) [arr addObject:right];
            if (width) [arr addObject:width];
            if (height) [arr addObject:height];
            [NSLayoutConstraint activateConstraints:arr];
        }
    });
}

+ (void)addLayoutConstraintsWithParentView:(UIView *)parentView
                               contentView:(UIView *)contentView
                                      left:(float)leftSpace
                                       top:(float)topSpace
                                     right:(float)rightSpace
                                    bottom:(float)bottomSpace
                                     width:(float)widthSpace
                                    height:(float)heightSpace {
    contentView.translatesAutoresizingMaskIntoConstraints = NO;//使用autolayout
    NSLayoutConstraint *top = nil;
    if (topSpace != INVLIAD_CONSTRAINT_VALUE) {
        top = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeTop multiplier:1 constant:topSpace];
    }
    
    NSLayoutConstraint *bottom = nil;
    if (bottomSpace != INVLIAD_CONSTRAINT_VALUE) {
        bottom = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeBottom multiplier:1 constant:-bottomSpace];
    }
    
    NSLayoutConstraint *left = nil;
    if (leftSpace != INVLIAD_CONSTRAINT_VALUE) {
        left = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeLeft multiplier:1 constant:leftSpace];
    }
    
    NSLayoutConstraint *right = nil;
    if (rightSpace != INVLIAD_CONSTRAINT_VALUE) {
        right = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeRight multiplier:1 constant:-rightSpace];
    }
    
    NSLayoutConstraint* width = nil;
    if (widthSpace != INVLIAD_CONSTRAINT_VALUE){
        width = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:widthSpace];
    }
    
    NSLayoutConstraint* height = nil;
    if (heightSpace != INVLIAD_CONSTRAINT_VALUE){
        height = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:heightSpace];
    }
    
    NSMutableArray* arr = [[NSMutableArray alloc] init];
    if (top) [arr addObject:top];
    if (bottom) [arr addObject:bottom];
    if (left) [arr addObject:left];
    if (right) [arr addObject:right];
    if (width) [arr addObject:width];
    if (height) [arr addObject:height];
    [NSLayoutConstraint activateConstraints:arr];
}



+(void)shareFile:(NSString *)path inVc:(UIViewController*)vc {
    NSURL *fileURL = [NSURL fileURLWithPath:path];
    //在iOS 11不显示分享选项了
    //定义URL数组
    NSArray *urls=@[fileURL];
    //创建分享的类型,注意这里没有常见的微信,朋友圈以QQ等,但是罗列完后,实际运行是相应按钮的,所以可以运行.
    
    UIActivityViewController *activituVC=[[UIActivityViewController alloc]initWithActivityItems:urls applicationActivities:nil];
    NSArray *cludeActivitys=@[
//                              UIActivityTypePostToFacebook,
//                              UIActivityTypePostToTwitter,
//                              UIActivityTypePostToWeibo,
//                              UIActivityTypePostToVimeo,
//                              UIActivityTypeMessage,
//                              UIActivityTypeMail,
//                              UIActivityTypeCopyToPasteboard,
//                              UIActivityTypePrint,
//                              UIActivityTypeAssignToContact,
//                              UIActivityTypeSaveToCameraRoll,
//                              UIActivityTypeAddToReadingList,
//                              UIActivityTypePostToFlickr,
//                              UIActivityTypePostToTencentWeibo
                              ];
    activituVC.excludedActivityTypes=cludeActivitys;
    //显示分享窗口
    [vc presentViewController:activituVC animated:YES completion:nil];

}
+(void)shareUrl:(NSString *)urlStr inVc:(UIViewController*)vc {
    NSURL *fileURL = [NSURL URLWithString:urlStr];
    //在iOS 11不显示分享选项了
    //定义URL数组
    NSArray *urls=@[fileURL];
    //创建分享的类型,注意这里没有常见的微信,朋友圈以QQ等,但是罗列完后,实际运行是相应按钮的,所以可以运行.
    
    UIActivityViewController *activituVC=[[UIActivityViewController alloc]initWithActivityItems:urls applicationActivities:nil];
    NSArray *cludeActivitys=@[];
    activituVC.excludedActivityTypes=cludeActivitys;

    //显示分享窗口
    [vc presentViewController:activituVC animated:YES completion:nil];
}

@end
